package org.linlinjava.litemall.core.kdniao.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.linlinjava.litemall.core.kdniao.util.KdniaoUtil;
import org.linlinjava.litemall.core.kdniao.dto.KdniaoCreateOrderDto;
import org.linlinjava.litemall.core.kdniao.dto.KdniaoCreateOrderResultDto;
import org.linlinjava.litemall.core.kdniao.dto.KdniaoOrderTraceResultDto;
import org.linlinjava.litemall.core.kdniao.dto.Order;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class KdniaoService {

    /**
     * Json方式 即时查询订单物流轨迹
     *
     * @throws Exception
     */
    public KdniaoOrderTraceResultDto getOrderTracesByJson(Order order) throws Exception {
        if(order.getOrderCode() == null) {
            order.setOrderCode("");
        }
        String requestData = JSON.toJSONString(order);

        Map<String, String> params = new HashMap<String, String>();
        params.put("RequestData", KdniaoUtil.urlEncoder(requestData, "UTF-8"));
        params.put("EBusinessID", KdniaoUtil.EBUSINESS_ID);
        params.put("RequestType", "8001");
        String dataSign = KdniaoUtil.encrypt(requestData, KdniaoUtil.APPKEY, "UTF-8");
        params.put("DataSign", KdniaoUtil.urlEncoder(dataSign, "UTF-8"));
        params.put("DataType", "2");

        String result = KdniaoUtil.sendPost(KdniaoUtil.TRACK_QUERY_FORMAL_REQURL, params);
        KdniaoOrderTraceResultDto kdniaoOrderTraceResultDto = JSONObject.parseObject(result, KdniaoOrderTraceResultDto.class);

        //根据公司业务处理返回的信息......

        return kdniaoOrderTraceResultDto;
    }


    /**
     * Json方式 在线下单
     *
     * @throws Exception
     */
    public KdniaoCreateOrderResultDto orderOnlineByJson(KdniaoCreateOrderDto kdniaoCreateOrderDto) throws Exception {

        String requestData = JSON.toJSONString(kdniaoCreateOrderDto);

        Map<String, String> params = new HashMap<String, String>();
        params.put("RequestData", KdniaoUtil.urlEncoder(requestData, "UTF-8"));
        params.put("EBusinessID", KdniaoUtil.EBUSINESS_ID);
        params.put("RequestType", "1001");
        String dataSign = KdniaoUtil.encrypt(requestData, KdniaoUtil.APPKEY, "UTF-8");
        params.put("DataSign", KdniaoUtil.urlEncoder(dataSign, "UTF-8"));
        params.put("DataType", "2");

        String result = KdniaoUtil.sendPost(KdniaoUtil.CREATE_ORDER_FORMAL_REQURL, params);
        KdniaoCreateOrderResultDto kdniaoCreateOrderResultDto = JSONObject.parseObject(result, KdniaoCreateOrderResultDto.class);

        //根据公司业务处理返回的信息......

        return kdniaoCreateOrderResultDto;
    }


    /**
     * Json方式  物流信息订阅(物流轨迹跟踪)
     *
     * @throws Exception
     */
    public String orderTracesSubByJson() throws Exception {
        String requestData = "{'OrderCode': 'SF201608081055208281'," +
                "'ShipperCode':'SF'," +
                "'LogisticCode':'3100707578976'," +
                "'PayType':1," +
                "'ExpType':1," +
                "'CustomerName':''," +
                "'CustomerPwd':''," +
                "'MonthCode':''," +
                "'IsNotice':0," +
                "'Cost':1.0," +
                "'OtherCost':1.0," +
                "'Sender':" +
                "{" +
                "'Company':'LV','Name':'Taylor','Mobile':'15018442396','ProvinceName':'上海','CityName':'上海','ExpAreaName':'青浦区','Address':'明珠路73号'}," +
                "'Receiver':" +
                "{" +
                "'Company':'GCCUI','Name':'Yann','Mobile':'15018442396','ProvinceName':'北京','CityName':'北京','ExpAreaName':'朝阳区','Address':'三里屯街道雅秀大厦'}," +
                "'Commodity':" +
                "[{" +
                "'GoodsName':'鞋子','Goodsquantity':1,'GoodsWeight':1.0}]," +
                "'Weight':1.0," +
                "'Quantity':1," +
                "'Volume':0.0," +
                "'Remark':'小心轻放'}";

        Map<String, String> params = new HashMap<String, String>();
        params.put("RequestData", KdniaoUtil.urlEncoder(requestData, "UTF-8"));
        params.put("EBusinessID", KdniaoUtil.EBUSINESS_ID);
        params.put("RequestType", "1008");
        String dataSign = KdniaoUtil.encrypt(requestData, KdniaoUtil.APPKEY, "UTF-8");
        params.put("DataSign", KdniaoUtil.urlEncoder(dataSign, "UTF-8"));
        params.put("DataType", "2");

        String result = KdniaoUtil.sendPost(KdniaoUtil.SUBSCRIBE_FORMAL_REQURL, params);

        //根据公司业务处理返回的信息......

        return result;
    }

    /**
     * Json方式 单号识别
     *
     * @throws Exception
     */
    public String getOrderTracesByJson(String expNo) throws Exception {
        String requestData = "{'LogisticCode':'" + expNo + "'}";

        Map<String, String> params = new HashMap<String, String>();
        params.put("RequestData", KdniaoUtil.urlEncoder(requestData, "UTF-8"));
        params.put("EBusinessID", KdniaoUtil.EBUSINESS_ID);
        params.put("RequestType", "2002");
        String dataSign = KdniaoUtil.encrypt(requestData, KdniaoUtil.APPKEY, "UTF-8");
        params.put("DataSign", KdniaoUtil.urlEncoder(dataSign, "UTF-8"));
        params.put("DataType", "2");

        String result = KdniaoUtil.sendPost(KdniaoUtil.ORDER_DISTINGUISH_FORMAL_REQURL, params);

        //根据公司业务处理返回的信息......

        return result;
    }

}
